<?php
/**
 * 游戏奖品管理
 * User: ws
 * Date: 2018/10/8
 * Time: 10:10
 */
//好烦躁，滚蛋吧皮卡丘
namespace app\admin\controller;

use app\admin\model\GamesPrize as GamModeL;
use think\Db;
class Prize    extends Init
{
    function index(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            if(is_numeric(input('type')) === true){
                $where['type'] = input('type');
            }
            $where['name|stock|chance|desc'] = array('like','%'.trim($key).'%');
            $list = GamModeL::order('prize_id desc')->where($where)->paginate(input('limit'))->toArray();
            $url = config('aliyun_oss.url');

            foreach ($list['data'] as $key=>$itme){
                $list['data'][$key]['img'] = str_replace("/games",$url."games",$itme['img']);
            }

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list');
    }

    function add(){
        if(request()->isPost()){
            $data = $this->request->post();

            if(empty($data['img'])){
                return json(array('code'=>0,'msg'=>'添加失败,图片不能为空'));
            }
            if($data['prize_type']==0){
                if(empty($data['url'])){
                    return json(array('code'=>0,'msg'=>'添加失败,地址不能为空'));
                }
            }
            if (!GamModeL::create($data)) {
                return json(array('code'=>0,'msg'=>'添加失败'));
            }
            return json(array('code'=>200,'msg'=>'添加成功'));
        }

        return view('add');
    }

    function edit(){
        if(request()->isPost()){
            $data = $this->request->post();
            if($data['prize_type']==0){
                if(empty($data['url'])){
                    return json(array('code'=>0,'msg'=>'添加失败,地址不能为空'));
                }
            }
            if (!GamModeL::update($data)) {
                return json(array('code'=>0,'msg'=>'修改失败'));
            }
            return json(array('code'=>200,'msg'=>'修改成功'));
        }
        $url = config('aliyun_oss.url');

        $list = GamModeL::where('prize_id', input('id'))->find()->toArray();
        $list['img'] = str_replace("/games",$url."games",$list['img']);

        return view('edit',['list'=>$list]);
    }

    function del(){
        $id = input('param.id');
        $data = Db::name('winning_record')->where('prize_id',$id)->find();
        if($data){
            return json(array('code'=>0,'msg'=>'删除失败，已被存入抽奖记录不能被删除'));
        }
        if (!GamModeL::destroy(['prize_id' => $id])) {
            return json(array('code'=>0,'msg'=>'删除失败'));
        }
        return json(array('code'=>200,'msg'=>'删除成功'));
    }

    function index2(){
        if ($this->request->isAjax()) {
            $key = input('key')?input('key'):'';
            if(is_numeric(input('type')) === true){
                $where['w.type'] = input('type');
            }
            $where['u.name|g.name'] = array('like','%'.trim($key).'%');
            $list = Db::table('je_winning_record')->alias('w')->join('je_user u','w.user_id = u.user_id')->join('je_games_prize g','w.prize_id = g.prize_id')->field('u.name as uname,g.name as gname,g.prize_type,w.*')->where($where)->order('record_id desc')->paginate(input('limit'))->toArray();

            return json($result = ['code'=>0,'msg'=>'获取成功','data'=>$list['data'],'count'=>$list['total']]);
        }
        return view('list2');
    }

    function edit2(){
        if(request()->isPost()){
            $data = $this->request->post();

            if($data['type']==2){
                unset($data['type']);
                $retule = Db::name('winning_record')->where('record_id',$data['record_id'])->setField('state', 0);
                unset($data['record_id']);
                $data['receive_time'] = time();
                $retule = Db::name('coupons_use')->insert($data);
                Db::name('coupons')->where('coupons_id', $data['coupons_id'])->setInc('receive_number', 1);
            }elseif($data['type']==1){
                if(empty($data['numbers']))return json(array('code'=>0,'msg'=>'发奖失败，快递单号不能为空'));
                        $rec['shipping_id'] = $data['shipping_id'];
                        $rec['numbers'] = $data['numbers'];
                        $rec['state'] = 0;
                $retule = Db::name('winning_record')->where('record_id',$data['record_id'])->setField($rec);

            }elseif($data['type']==0){
                $retule = Db::name('winning_record')->where('record_id',$data['record_id'])->setField('state', 0);
            }

            if (!$retule) {
                return json(array('code'=>0,'msg'=>'发奖失败'));
            }
            return json(array('code'=>200,'msg'=>'发奖成功'));
        }


        $list = Db::table('je_winning_record')->alias('w')->join('je_games_prize g','w.prize_id = g.prize_id')->field('g.prize_type,w.record_id,w.user_id')->where('record_id', input('id'))->find();

        if($list['prize_type']==1){
            $list['coupons'] = Db::name('coupons')->where('state = 1 and area = 1')->order('coupons_id desc')->field('name,coupons_id,number,receive_number')->select();
            foreach($list['coupons'] as $key=>$val){
                if($val['receive_number']==$val['number']){
                    unset($list['coupons'][$key]);
                }
            }
        }else if($list['prize_type']==2){
            $list['shipping'] = Db::name('shipping')->field('name,shipping_id')->select();
        }

        return view('edit2',['list'=>$list]);
    }
}